<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Pubfun extends FOX_Controller {

    function __construct ()
    {
        parent::__construct();
        $this->load->model('upload_m');
        $this->load->model('attachs_m');
    }

    public function editor_upload_img(){


        $type = ($this->input->get('type'))?$this->input->get('type'):'shop';
        $shopid = ($this->input->get('shopid'))?$this->input->get('shopid'):'';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name'] = TRUE;
        $config['max_size'] = 3000;
        if(!empty($type) && !empty($shopid)){
            $base_path = FCPATH .'uploads/'.$type.'/';
            $config['upload_path'] = FCPATH .'uploads/'.$type.'/'.$shopid."/";
            $pic_path = base_url('uploads/'.$type.'/'.$shopid."/");
            if(!file_exists($base_path)){
                mkdir($base_path,0777,true);
                if(!file_exists($config['upload_path'])){
                    mkdir($config['upload_path'],0777,true);
                }
            }
            //原文件名
            $file_name = $_FILES['imgFile']['name'];
            //获得文件扩展名
            $temp_arr = explode(".", $file_name);
            $file_ext = array_pop($temp_arr);
            $file_ext = trim($file_ext);
            $file_ext = strtolower($file_ext);
            //服务器上临时文件名
            $tmp_name = $_FILES['imgFile']['tmp_name'];
            //新文件名
            $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
            //移动文件
            $file_path = $config['upload_path'] . $new_file_name;
            if (move_uploaded_file($tmp_name, $file_path) === false) {
                alert("上传文件失败。");
            }
            @chmod($file_path, 0644);
            $file_url = $pic_path . $new_file_name;

            header('Content-type: text/html; charset=UTF-8');
            echo json_encode(array('error' => 0, 'url' => $file_url));
            exit;
        }

    }

    public function upload_img(){
        $good_id = $this->input->get('good_id',TRUE);
        $access_key = $this->input->get('access_key',TRUE);
        if($_FILES){
            // 数组合并
            $len = count($_FILES['pic']['name']);
            for ($i=0; $i < $len; $i++) { 
                    foreach ($_FILES['pic'] as $key => $value) {
                        $data[$key] = $value[$i];
                    }
                    $pics[] = $data;
                }
            $_FILES = $pics;
            if(empty($access_key)){
                $access_key = md5($this->session->userdata('user_id').time().uniqid());
            }
            if(!empty($pics)){
                $config['upload_path'] = FCPATH .'uploads/goods/'.$access_key."/";
                $config['allowed_types'] = 'gif|jpg|png';
                $config['encrypt_name'] = TRUE;
                $config['max_size'] = 3000;
                if(!file_exists($config['upload_path'])){
                    mkdir($config['upload_path'],0777,true);
                }
                $this->load->library('upload', $config);
                foreach ($pics as $key => $value) {
                    if(!$this->upload->do_upload($key)) {
                            echo '上传出错了';
                        }
                    $upload_data = $this->upload->data();
                    $attachs['file_name']=$upload_data['file_name'];
                    $attachs['access_key']=$access_key;
                    $attachs['add_time']=time();
                    $attachs['file_location']='uploads/goods/'.$access_key."/";
                    $attachs['item_id']=$good_id;
                    $attachs['is_image']=1;
                    $attachs['item_type']=1;
                    $this->attachs_m->add_attachs($attachs);
                }
                $arr = array ('status'=>1,'msg'=>'图片上传成功','access_key'=>$access_key);
                    echo json_encode($arr);
                    exit;
            }else{
                $arr = array ('status'=>0,'msg'=>strip_tags($this->upload->display_errors()),'icon'=>5,'code'=>$this->security->get_csrf_hash());
                echo json_encode($arr);
                exit;
            }
        }else{
            $arr = array ('status'=>0,'msg'=>'图片上传失败','icon'=>5,'code'=>$this->security->get_csrf_hash());
            echo json_encode($arr);
            exit;
        }
    }

    /**
     * 删除图片
     * [deltet_img id]
     * @return [type] [description]
     */
    public function delete_img()
    {
        $attach_id = $this->input->post('key',TRUE);
        if($attach_id){
            $pic = $this->attachs_m->get_attachs_by_id($attach_id);
            if(file_exists(FCPATH .$pic['file_location'].$pic['file_name'])){
                    unlink(FCPATH .$pic['file_location'].$pic['file_name']);
                }
            $del_res = $this->attachs_m->delete_attachs_by(['id'=>$attach_id]);
            $arr = array ('status'=>1,'msg'=>'图片删除成功');
            echo json_encode($arr);
            exit;
        }else{
            return false;
        }
    }
}
